package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.config.MyThreadConfig;
import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.entity.vo.SkuItemVo;
import com.atguigu.gulimall.product.service.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;

@Slf4j
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
  //  private final Logger logger = LoggerFactory.getLogger(SkuInfoServiceImpl.class);
    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> spu = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return spu;
    }


    @Override
    public SkuItemVo skuInfo(Long skuId) {
        SkuItemVo skuItemVo=new SkuItemVo();

        //获取spu Entity
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        },threadPoolExecutor).exceptionally((ex)->{
            return null;
        });


        //spu图片信息
        CompletableFuture<Void> skuImageFuture = infoFuture.thenAcceptAsync((e) -> {
            skuItemVo.setImages(skuImagesService.getImagesBySkuId(e.getSkuId()));
        }, threadPoolExecutor).exceptionally((ex)->{
            return null;
        });


        //spu销售属性
        CompletableFuture<Void> skuSaleAttrFuture = infoFuture.thenAcceptAsync((e) -> {
            skuItemVo.setSaleAttr(skuSaleAttrValueService.getSaleAttrsBySpuId(e.getSpuId()));
        }, threadPoolExecutor).exceptionally((ex)->{
            return null;
        });

        //获取spu描述
        CompletableFuture<Void> InfoDescFuture = infoFuture.thenAcceptAsync((e) -> {
            skuItemVo.setDesc(spuInfoDescService.getById(e.getSpuId()));
        }, threadPoolExecutor).exceptionally((ex)->{
            return null;
        });

        //获取 spu 的规格参数信息
        CompletableFuture<Void> attrGroupFuture = infoFuture.thenAcceptAsync((e) -> {
            skuItemVo.setGroupAttrs(attrGroupService.getAttrGroupWithAttrsBySpuId(e.getSpuId(), e.getCatalogId()));
        }, threadPoolExecutor).exceptionally((ex)->{
            return null;
        });


        try {
            CompletableFuture.allOf(skuImageFuture,skuSaleAttrFuture,InfoDescFuture,attrGroupFuture).get();
        } catch (InterruptedException | ExecutionException e) {
            log.info("商品详情页查询错误");
            e.printStackTrace();
        }

        return skuItemVo;
    }

}